// pages/news/news.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
// index/list.js
Page({
    data: {
        tabbar: {},
        show: false,//控制下拉列表的显示隐藏，false隐藏、true显示
        show1: false,
        show2: false,
        selectData: ['产品类型', '增值贷', '安居分'],//下拉列表的数据
        index: 0,//选择的下拉列表下标
        selectData1: ['近三个月', '近半年', '近一年', '全部'],//下拉列表的数据
        projectList: [],
        index1: 0,//选择的下拉列表下标
        teamTermList: [],
        index2: 0,//选择的下拉列表下标
        productId: "",
        dayIndex: "",
        agentId: "",
        teamPartnerStatus: "",
        token: "",
        projectModeLength: "",
        pageSize: 30,
        pageNo: 1,
        approvalData: []
    },
    // 点击下拉显示框
    selectTap() {
        this.setData({
            show: !this.data.show,
            show1: false,
            show2: false
        });
    },
    // 点击下拉显示框
    selectTap1() {
        this.setData({
            show1: !this.data.show1,
            show: false,
            show2: false
        });
    },
    // 点击下拉显示框
    selectTap2() {
        this.setData({
            show2: !this.data.show2,
            show1: false,
            show: false
        });
    },
    // 点击下拉列表
    optionTap(e) {
        let productId = e.currentTarget.dataset.id;//获取点击的下拉列表的id
        let productIndex = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
        let dayIndex = this.data.dayIndex
        let agentId = this.data.agentId
        this.setData({
            productId: productId,
            pageNo: 1,
            pageSize: 30
        })
        let pageNo = this.data.pageNo
        let pageSize = this.data.pageSize
        this.gitData(dayIndex, productId, agentId, pageNo, pageSize)
        this.setData({
            index: productIndex,
            show: !this.data.show
        });
    },
    // 点击日期下拉列表
    optionTap1(e) {
        let dayIndex = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
        let productId = this.data.productId
        let agentId = this.data.agentId
        this.setData({
            dayIndex: dayIndex,
            pageNo: 1,
            pageSize: 30
        })
        let pageNo = this.data.pageNo
        let pageSize = this.data.pageSize
        this.gitData(dayIndex, productId, agentId, pageNo, pageSize)
        this.setData({
            index1: dayIndex,
            show1: !this.data.show1
        });
    },
    // 点击经纪人下拉列表
    optionTap2(e) {
        let agentId = e.currentTarget.dataset.id;
        let agentIndex = e.currentTarget.dataset.index;
        console.log(agentIndex)
        let productId = this.data.productId;
        let dayIndex = this.data.dayIndex
        let pageNo = this.data.pageNo
        let pageSize = this.data.pageSize
        this.gitData(dayIndex, productId, agentId, pageNo, pageSize)
        this.setData({
            index2: agentIndex,
            show2: !this.data.show2,
            agentId: agentId,
            pageNo: 1,
            pageSize: 30
        });
    },
    // 点击nav
    navShow() {
        this.setData({
            show: false,
            show1: false,
            show2: false
        })
    },
    // 点击card
    cardbtn(obj) {
        this.setData({
            show: false,
            show1: false,
            show2: false
        })
        let orderNumber = obj.currentTarget.dataset.id
        let belongId = obj.currentTarget.dataset.belongid
        wx.setStorageSync("orderNumber", orderNumber)
        wx.setStorageSync("belongId", belongId)
            wx.navigateTo({
                url: '/pages/wallet/recharge',
            })
    },
    /**
     * 页面的初始数据
     */
    // data: {
    //     select: false,
    //     tihuoWay: '全部',
    // },
    news: function () {
        this.setData({
            show: false,
            show1: false,
            show2: false
        })
        wx.navigateTo({
            url: '/pages/order/order?id=' + this.data.productId,
        })
    },
    // 分享
    onShareAppMessage: function (res) {
        var that = this;
        return {
            title: '贷投狗小程序',
            path: 'pages/wallet/wallet',
            success: function (res) {
                that.shareClick();
            },
            fail: function (res) {
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.editTabbar();
        let token = wx.getStorageSync('token')
        if (token) {
            // util.isLoginModal();
            var that = this;
            let orderStatus = wx.getStorageSync("orderStatus")
            that.setData({
                teamPartnerStatus: wx.getStorageSync("teamPartnerStatus"),
                token: wx.getStorageSync('token'),
                projectModeLength: wx.getStorageSync('projectModeLength')
            })
            // let dayIndex = that.data.dayIndex
            // let productId = that.data.productId
            // let agentId = that.data.agentId
            // that.getIndexData(dayIndex, productId, agentId);
        }
    },
    onShow: function () {
        this.setData({
            pageNo: 1,
            pageSize: 30,
            show: false,
            show1: false,
            show2: false
        })
        let token = wx.getStorageSync('token')
        if (!token) {
            wx.showModal({
                title: '提示',
                content: '请先登录后查看',
                showCancel: true,//是否显示取消按钮
                cancelText: "取消",//默认是“取消”
                confirmText: "去登录",
                success: function (res) {
                    if (res.cancel) {
                        //点击取消,默认隐藏弹框
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    } else {
                        //点击确定
                        wx.redirectTo({
                            url: '/pages/account/login',
                        })
                    }
                },
            })
        } else {
            this.gitList();
            let dayIndex = this.data.dayIndex
            let productId = this.data.productId
            let agentId = this.data.agentId
            let pageNo = this.data.pageNo
            let pageSize = this.data.pageSize
            this.getIndexData(dayIndex, productId, agentId, pageNo, pageSize)
        }
    },
    onUnload() {

    },
    // 时间
    bindShowMsg() {
        this.setData({
            select: !this.data.select,
            selectone: false
        })
    },
    mySelect(e) {
        var name = e.currentTarget.dataset.name
        this.setData({
            tihuoWay: name,
            select: false
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onPullDownRefresh() {
        wx.showNavigationBarLoading();
        this.setData({
            approvalData: [],
            pageNo: 1
        })
        let dayIndex = this.data.dayIndex
        let productId = this.data.productId
        let agentId = this.data.agentId
        let pageNo = this.data.pageNo
        let pageSize = this.data.pageSize
        this.getIndexData(dayIndex, productId, agentId, pageNo, pageSize)
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    // 下拉请求数据
    more: function () {
        this.onReachBottom()
    },
    // 上拉刷新
    onReachBottom() {
        //做些什么
        var that = this
        wx.showNavigationBarLoading()
        let pageNo = that.data.pageNo + 1
        that.setData({
            pageNo: pageNo
        })
        let dayIndex = this.data.dayIndex
        let productId = this.data.productId
        let agentId = this.data.agentId
        let pageSize = that.data.pageSize
        this.getIndexData(dayIndex, productId, agentId, pageNo, pageSize)
    },
    gitList: function () {
        var that = this;
        // 产品类型数据
        util.https(app.globalData.api + "/applet/order/condition", "GET", {},
            function (data) {
                if (data.code == 200) {
                    let arr = data.data;
                    arr.unshift({ id: '', productName: '全部' })
                    that.setData({
                        projectList: arr
                    })
                } else {
                    let list = [{ id: '', productName: '全部' }]
                    that.setData({
                        projectList: list
                    })
                    util.toolTip(that, data.message)
                }
            }
        )
    },
    gitData: function (dayIndex, productId, agentId, pageNo, pageSize) {
        var that = this;
        if (!dayIndex) {
            dayIndex = 0
        }
        let query = {}
        query.dayIndex = dayIndex
        query.productId = productId
        query.agentId = agentId
        query.pageNo = pageNo
        query.pageSize = pageSize
        if (!productId) {
            delete query.productId
        }
        if (!agentId) {
            delete query.agentId
        }
        // 订单数据
        util.https(app.globalData.api + "/applet/order/listByUser", "GET", query,
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        approvalData: data.data.records
                    })
                } else {
                    util.toolTip(that, data.message)
                }
            }
        )
    },
    // 获取数据
    getIndexData: function (dayIndex, productId, agentId, pageNo, pageSize) {
        let teamPartnerStatus = wx.getStorageSync("teamPartnerStatus")
        var that = this;
        if (!dayIndex) {
            dayIndex = 0
        }
        let query = {}
        query.dayIndex = dayIndex
        query.productId = productId
        query.agentId = agentId
        query.pageNo = pageNo
        query.pageSize = pageSize
        if (!productId) {
            delete query.productId
        }
        if (!agentId) {
            delete query.agentId
        }
        // 订单数据
        util.https(app.globalData.api + "/applet/order/listByUser", "GET", query,
            function (data) {
                if (data.code == 200) {
                    if (query.pageNo == 1) {
                        that.setData({
                            approvalData: data.data.records
                        })
                    } else if (query.pageNo != 1 && data.data.records != null) {
                        let Data = that.data.approvalData.concat(data.data.records)
                        that.setData({
                            approvalData: Data,
                            title: '下拉查看更多',
                        })
                    } else {
                        that.setData({
                            title: '暂无更多数据',
                        })
                    }
                } else {
                    util.toolTip(that, data.message)
                }
            }
        )
        // 获取全部经纪人数据
        if (teamPartnerStatus == 1) {
            util.https(app.globalData.api + "/applet/order/teamTerm", "GET", {},
                function (data) {
                    if (data.code == 200) {
                        let arr = data.data;
                        arr.unshift({ id: '', name: '全部经纪人' })
                        that.setData({
                            teamTermList: arr
                        })
                    } else {
                        util.toolTip(that, data.message)
                    }
                }
            )
        }
    }
})